﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace DotNet.Common
{
    public class JsUtil:System.Web.UI.Page
    {
        /// <summary>
        /// 弹出消息框
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowMsg(string msg)
        {
            string js = @"<Script language='JavaScript'>
                    alert('" + msg + "');</Script>";
            HttpContext.Current.Response.Write(js);
            
        }

        /// <summary>
        /// 弹出消息框并且转向到新的URL
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="toURL"></param>
        public static void ShowMsg(string msg, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, msg, toURL));
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 回到历史页面
        /// </summary>
        /// <param name="value">-1/1</param>
        public static void GoHistory(int value)
        {
            string js = @"<Script language='JavaScript'>
                    history.go({0});  
                  </Script>";
            HttpContext.Current.Response.Write(string.Format(js, value));
        }

        /// <summary>
        /// 关闭当前窗口
        /// </summary>
        public static void CloseWindow()
        {
            string js = @"<Script language='JavaScript'>
                    parent.opener=null;window.close();  
                  </Script>";
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 刷新父窗口
        /// </summary>
        /// <param name="url"></param>
        public static void RefreshParent(string url)
        {
            string js = @"<Script language='JavaScript'>
                    window.opener.location.href='" + url + "';window.close();</Script>";
            HttpContext.Current.Response.Write(js);
        }


        /// <summary>
        /// 刷新打开窗口
        /// </summary>
        public static void RefreshOpener()
        {
            string js = @"<Script language='JavaScript'>
                    opener.location.reload();
                  </Script>";
            HttpContext.Current.Response.Write(js);
        }


        /// <summary>
        /// 打开指定大小的新窗体
        /// </summary>
        /// <param name="url"></param>
        /// <param name="width"></param>
        /// <param name="heigth"></param>
        /// <param name="top"></param>
        /// <param name="left"></param>
        public static void OpenWindow(string url, int width, int heigth, int top, int left)
        {
            string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";

            HttpContext.Current.Response.Write(js);
        }
      

        /// <summary>
        /// 转向指定的Url
        /// </summary>
        /// <param name="url"></param>
        public static void LocationNewHref(string url)
        {
            string js = @"<Script language='JavaScript'>
                    window.location.replace('{0}');
                  </Script>";
            js = string.Format(js, url);
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 打开指定大小位置的模式对话框
        /// </summary>
        /// <param name="url"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="top"></param>
        /// <param name="left"></param>
        public static void ShowModalDialog(string url, int width, int height, int top, int left)
        {
            string features = "dialogWidth:" + width.ToString() + "px"
                + ";dialogHeight:" + height.ToString() + "px"
                + ";dialogLeft:" + left.ToString() + "px"
                + ";dialogTop:" + top.ToString() + "px"
                + ";center:yes;help=no;resizable:no;status:no;scroll=yes";
            string js = @"<script language=javascript>							
							showModalDialog('" + url + "','','" + features + "');</script>";
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 停留指定时间后，跳转到指定页
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="goUrl"></param>
        /// <param name="second"></param>
        public static void TipAndRedirect(string msg,string goUrl,string second)
        {
            HttpContext.Current.Response.Write("<meta http-equiv='refresh' content='"+second+";url=" + goUrl + "'>");
            HttpContext.Current.Response.Write("<br/><br/><p align=center><div style=\"size:12px\">&nbsp;&nbsp;&nbsp;&nbsp;" + msg.Replace("!","") + ",页面2秒内跳转!<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"" + goUrl + "\">如果没有跳转，请点击!</a></div></p>");
            HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.RegisterStartupScript("message", "<script language='javascript' defer>" + script + "</script>");
        }

        /// <summary>
        /// 加载js文件
        /// </summary>
        /// <param name="jsFilePath"></param>
        /// <param name="isInHead"></param>
        public static void IncludeJsFile(string jsFilePath, bool isInHead)
        {
            if (isInHead)
            {
                Page currentHandler = HttpContext.Current.CurrentHandler as Page;
                HtmlGenericControl child = new HtmlGenericControl("script");
                child.Attributes.Add("type", "text/javascript");
                child.Attributes.Add("language", "javascript");
                child.Attributes.Add("src", CommUtil.GetAppPath(1) + jsFilePath);
                currentHandler.Header.Controls.Add(child);
            }
            else
            {
                string str = string.Format("\n<script  type=\"text/javascript\" src=\"{0}\">\n</script>\n", jsFilePath);
                ((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(Type.GetType("System.String"), Guid.NewGuid().ToString(), str);
            }
        }
        /// <summary>
        /// 加载css样式表
        /// </summary>
        /// <param name="cssFilePath"></param>
        public static void IncludeCssFile(string cssFilePath)
        {
            HtmlGenericControl child = new HtmlGenericControl("link");
            child.Attributes.Add("href", cssFilePath);
            child.Attributes.Add("rel", "stylesheet");
            child.Attributes.Add("type", "text/css");
            Page handler = (Page)HttpContext.Current.Handler;
            handler.Header.Controls.Add(child);
        }

        /// <summary>
        /// 执行Js语句
        /// </summary>
        /// <param name="script"></param>
        /// <param name="isInHead"></param>
        public static void ExecuteJs(string script, bool isInHead)
        {
            string str = string.Format("\n<script  type=\"text/javascript\">\n{0}\n</script>\n", script);
            if (!isInHead)
            {
                ((Page)HttpContext.Current.Handler).ClientScript.RegisterStartupScript(Type.GetType("System.String"), Guid.NewGuid().ToString(), str);
            }
            else
            {
                HtmlGenericControl child = new HtmlGenericControl("script");
                child.Attributes.Add("type", "text/javascript");
                child.InnerHtml = "\n" + script + "\n";
                Page handler = (Page)HttpContext.Current.Handler;
                handler.Header.Controls.Add(child);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="script"></param>
        public static void ExecuteJs(string script)
        {
            ExecuteJs(script, true);
        }
        public static void JsAlert(string msg, bool isInHead)
        {
            string m = string.Format("alert(\"{0}\");", msg.Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("\n", @"\n"));
            ExecuteJs(m, isInHead);
        }

        public static void JsAlert(string msg)
        {
            JsAlert(msg, true);
        }
    }
}
